<?php
	$title='Fitness articles';
	$description='Fitness articles';
	$keywords='fitness, articles';
?>
<?php include('fns_db_articles.php'); ?>
<?php include('../views/layouts/_header.php'); ?>
<?php include('../views/layouts/_left.php'); ?>

<p class='heading1'>All articles ordered by date:</p>

<?php
if(checkIfAdmin()){
	print('<br/><a href="add_article.php">add article</a>');
}

// how many rows to show per page
$rowsPerPage = 10;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
	$pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;

$result = get_all_articles_ordered_by_date($offset, $rowsPerPage);


$myrow = mysqli_fetch_assoc ($result);

do {
	printf ("<br/><table>

	             <tr>
	             <td>
					 <p class='heading2'><a href='view_article.php?id=%s'>%s</a></p>
					 <p class='article_adds'>Date added: %s</p>
					 <p >Author: <span class='article_author'>%s</span></p>", $myrow["id"], $myrow["title"],$myrow["date"],$myrow["author"]);


	if(checkIfAdmin()){
		printf("<p class='article_adds'><a href='manage_articles.php?action=del&id=%s'>delete</a>&nbsp&nbsp
			<a href='edit_article.php?id=%s'>edit</a></p>", $myrow["id"], $myrow["id"]);
	}


	print("</td>
	         </tr>
	         </table>");

}while ($myrow = mysqli_fetch_assoc ($result));


// how many rows we have in database
$numrows = get_numrows_of_articles();

// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);

// print the link to access each page
$self = $_SERVER['PHP_SELF'];

// creating previous and next link
// plus the link to go straight to
// the first and last page

if ($pageNum > 1)
{
	$page  = $pageNum - 1;
	$prev  = " <a href=\"$self?page=$page\"><</a> ";

	$first = " <a href=\"$self?page=1\"><<</a> ";
}
else
{
	$prev  = '&nbsp;'; // we're on page one, don't print previous link
	$first = '&nbsp;'; // nor the first page link
}

if ($pageNum < $maxPage)
{
	$page = $pageNum + 1;
	$next = " <a href=\"$self?page=$page\">></a> ";

	$last = " <a href=\"$self?page=$maxPage\">>></a> ";
}
else
{
	$next = '&nbsp;'; // we're on the last page, don't print next link
	$last = '&nbsp;'; // nor the last page link
}

// print the navigation link
echo $first . $prev ."  page $pageNum of $maxPage  " . $next . $last;

?>

<?php include('../views/layouts/_footer.php'); ?>